package StudyCode.summary.mycollection;

import java.lang.reflect.Array;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class TransposeTwoDimensionalObjectArray {
	public static Object[][] transpose(Object[][] array) {
		if (array == null || array.length == 0 || array[0].length == 0) {
			throw new RuntimeException("Null or empty array");
		}

		Object[][] transposed = (Object[][]) Array.newInstance(array[0]
				.getClass().getComponentType(), new int[]{
				array[0].length, array.length});

		for (int i = 0; i < transposed.length; i++) {
			for (int j = 0; j < array.length; j++) {
				transposed[i][j] = array[j][i];
			}
		}

		return transposed;
	}
}
